﻿using UnityEngine;
using System.Collections;

public class IntroTruck : MonoBehaviour {

    float m_fSpeed;
    float m_fAccel;

    bool m_bTrumbleUp;

    bool m_bParked;
    public bool m_bAllEmbarked;

    public bool m_bStop;

	// Use this for initialization
	void Start () {
        if (m_bStop)
        {
            m_fSpeed = 0.75f * Time.deltaTime;
        }
        else
        {
            m_fSpeed = 1.0f * Time.deltaTime;
        }
        m_fAccel = 0.0001f * Time.deltaTime;
	}
	
	// Update is called once per frame
    void Update()
    {
        if (transform.localPosition.x < -3.4f)
        {
            Destroy(gameObject);
        }

        if (!m_bParked)
        {
            transform.localPosition = new Vector3(transform.localPosition.x - m_fSpeed, transform.localPosition.y);

            if (m_bStop)
            {
                if (transform.localPosition.x < 1.90f)
                {
                    if (m_fSpeed > 0)
                    {
                        m_fSpeed -= m_fAccel;
                        m_fAccel += 0.00006f * Time.deltaTime;
                        if (m_fSpeed < 0.0075f)
                            Trumble();
                    }
                    else
                    {
                        m_fSpeed = 0;
                        m_bParked = true;
                        GameObject.Find("IntroScene").GetComponent<IntroScene>().m_bEmbark_Start = true;
                    }
                }
            }
        }
        else
        {
            if (m_bAllEmbarked)
            {
                transform.localPosition = new Vector3(transform.localPosition.x - m_fSpeed, transform.localPosition.y);

                if (m_fSpeed < 0.4f)
                {
                    m_fSpeed += m_fAccel;
                    m_fAccel += 0.00002f * Time.deltaTime;
                    if (m_fSpeed < 0.0055f)
                        Trumble();
                }
                else
                {
                    m_fSpeed = 0.4f;
                }
            }
        }
    }

    void Trumble()
    {
        if (!m_bTrumbleUp)
        {
            if (transform.localPosition.y > -1.3005f)
                transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y - (0.4f * Time.deltaTime - m_fSpeed));
            else
            {
                transform.localPosition = new Vector3(transform.localPosition.x, -1.3005f);
                m_bTrumbleUp = true;
            }
        }
        else
        {
            if (transform.localPosition.y < -1.2995)
                transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + (0.4f * Time.deltaTime - m_fSpeed));
            else
            {
                transform.localPosition = new Vector3(transform.localPosition.x, -1.2995f);
                m_bTrumbleUp = false;
            }
        }

    }
}
